﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.User
{
    public partial class ManageTask2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DepartmentDAO departmentDao = new DepartmentDAO();
                BLDepartment filter = new BLDepartment();
                filter.Id = Guid.Empty;
                filter.Name = string.Empty;
                filter.Parent = new BLDepartment();
                filter.Parent.Id = Guid.Empty;
                List<BLDepartment> depts = departmentDao.search(filter);
                depts.Insert(0, filter);
                ddlDepartment.DataSource = depts;
                ddlDepartment.DataBind();

                loadTasks();
                dgvTasks.EmptyDataText = "No tasks";
            }
        }

        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Guid deptId = new Guid(ddlDepartment.SelectedValue);
                List<BLRole> roles = new List<BLRole>();
                BLRole filter = new BLRole();
                filter.Id = Guid.Empty;
                filter.Name = string.Empty;
                filter.Department = new BLDepartment();
                filter.Department.Id = deptId;
                if (!deptId.Equals(Guid.Empty))
                {
                    RoleDAO roleDao = new RoleDAO();
                    roles = roleDao.searchRolesInDepartment(filter);
                }
                roles.Insert(0, filter);
                ddlRole.DataSource = roles;
                ddlRole.DataBind();

                ddlUser.DataSource = new List<BLUser>();
                ddlUser.DataBind();

                loadTasks();
            }
            catch (Exception exp)
            {
                Console.Write(exp.StackTrace);
            }
        }

        private void loadUsers()
        {
            try
            {
                string roleName = ddlRole.SelectedItem.Text;
                List<BLShortentUser> users = new List<BLShortentUser>();
                BLShortentUser empty = new BLShortentUser();
                empty.Id = Guid.Empty;
                empty.UserName = string.Empty;
                users.Add(empty);
                if (!string.IsNullOrEmpty(roleName))
                {
                    string[] usernames = Roles.GetUsersInRole(roleName);
                    foreach (string un in usernames)
                    {
                        BLShortentUser row = new BLShortentUser();
                        MembershipUser user = Membership.GetUser(un);
                        row.Id = (Guid)user.ProviderUserKey;
                        row.UserName = user.UserName;
                        users.Add(row);
                    }
                }
                ddlUser.DataSource = users;
                ddlUser.DataBind();

                loadTasks();
            }
            catch (Exception exp)
            {
                Console.Write(exp.StackTrace);
            }
        }

        private void loadTasks()
        {
            TaskDAO dao = new TaskDAO();
            BLTaskFilter filter = new BLTaskFilter();
            filter.Id = Guid.Empty;
            filter.Name = string.Empty;
            filter.Type = new BLTaskType();
            filter.Type.Id = Guid.Empty;
            Guid inChargeUser = Guid.Empty;
            try
            {
                inChargeUser = new Guid(ddlUser.SelectedValue);
            }
            catch (Exception exp)
            {
                Console.Write(exp.StackTrace);
            }
            filter.InChargeUserId = inChargeUser;

            DateTime date;
            if (cbxEstimateStart.Checked)
            {
                if (DateTime.TryParse(tbxEstimateStartFrom.Text, out date))
                {
                    filter.EstimateStartDateDown = date;
                }
                else
                {
                    filter.EstimateStartDateDown = Constant.DefaultMinDate;
                }
                if (DateTime.TryParse(tbxEstimateStartTo.Text, out date))
                {
                    filter.EstimateStartDateUp = date;
                }
                else
                {
                    filter.EstimateStartDateUp = Constant.DefaultMaxDate;
                }
            }
            else
            {
                filter.EstimateStartDateDown = Constant.DefaultMinDate;
                filter.EstimateStartDateUp = Constant.DefaultMaxDate;
            }

            if (cbxEstimateFinish.Checked)
            {
                if (DateTime.TryParse(tbxEstimateFinishFrom.Text, out date))
                {
                    filter.EstimateFinishDateDown = date;
                }
                else
                {
                    filter.EstimateFinishDateDown = Constant.DefaultMinDate;
                }
                if (DateTime.TryParse(tbxEstimateFinishTo.Text, out date))
                {
                    filter.EstimateFinishDateUp = date;
                }
                else
                {
                    filter.EstimateFinishDateUp = Constant.DefaultMaxDate;
                }
            }
            else
            {
                filter.EstimateFinishDateDown = Constant.DefaultMinDate;
                filter.EstimateFinishDateUp = Constant.DefaultMaxDate;
            }

            if (cbxRealStart.Checked)
            {
                if (DateTime.TryParse(tbxRealStartFrom.Text, out date))
                {
                    filter.RealStartDateDown = date;
                }
                else
                {
                    filter.RealStartDateDown = Constant.DefaultMinDate;
                }
                if (DateTime.TryParse(tbxRealStartTo.Text, out date))
                {
                    filter.RealStartDateUp = date;
                }
                else
                {
                    filter.RealStartDateUp = Constant.DefaultMaxDate;
                }
            }
            else
            {
                filter.RealStartDateDown = Constant.DefaultMinDate;
                filter.RealStartDateUp = Constant.DefaultMaxDate;
            }

            if (cbxRealFinish.Checked)
            {
                if (DateTime.TryParse(tbxRealFinishFrom.Text, out date))
                {
                    filter.RealFinishDateDown = date;
                }
                else
                {
                    filter.RealFinishDateDown = Constant.DefaultMinDate;
                }
                if (DateTime.TryParse(tbxRealFinishTo.Text, out date))
                {
                    filter.RealFinishDateUp = date;
                }
                else
                {
                    filter.RealFinishDateUp = Constant.DefaultMaxDate;
                }
            }
            else
            {
                filter.RealFinishDateDown = Constant.DefaultMinDate;
                filter.RealFinishDateUp = Constant.DefaultMaxDate;
            }

            List<BLTask> pre = dao.searchByUser(filter);

            dgvTasks.DataSource = pre;
            dgvTasks.DataBind();
        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadUsers();
        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadTasks();
        }

        protected void dgvTasks_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dgvTasks.PageIndex = e.NewPageIndex;
            loadTasks();
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            dgvTasks.PageIndex = 0;
            loadTasks();
        }
    }
}
